<?php
/**
 * Copyright (c) 2006-2007, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class SecurityComponent
{
	function startup(Controller $Controller)
	{
		$this->data =& $Controller->data;
	}
	
	/**
	 * Restricts $data to certain pairs of models/fields.
	 *
	 * @param $args array($model1 => '$fields1', $model2 => '$fields2');
	 */
	function restrictFieldsTo($args)
	{
		foreach ($args as $model => $fields)
		{
			if (empty($this->data[$model]))
				continue;
			
			if (!is_array($fields))
				$fields = explode(',', str_replace(' ', '', $fields));
			
			foreach($this->data[$model] as $f => $v)
			{
				if (!in_array($f, $fields))
					unset($this->data[$model][$f]);
			}
		}
	}
	
	/**
	 * Removes certain pairs of models/fields.
	 * 
	 * @param $args array($model1 => '$fields1', $model2 => '$fields2');
	 */
	function removeFields($args)
	{
		foreach ($args as $model => $fields)
		{
			if (empty($this->data[$model]))
				continue;
			
			if (!is_array($fields))
				$fields = explode(',', str_replace(' ', '', $fields));
			
			foreach($fields as $f)
				unset($this->data[$model][$f]);
		}
	}
}
?>